package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.dao.*;
import com.qf.entity.*;
import com.qf.service.TypeService;
import com.qf.utils.QiNiuUpload;
import com.qf.utils.ResultPage;
import com.qf.service.CourseService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * @author 王希卓
 * @Date 2023/7/17 14:21
 * 课程业务实现
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseDao,TbCourse> implements CourseService {

    @Autowired(required = false)
    private CourseDao courseDao;
    @Autowired(required = false)
    TeacherDao teacherDao;
    @Autowired(required = false)
    TypeDao typeDao;
    @Autowired(required = false)
    ChapterDao chapterDao;
    @Autowired(required = false)
    ResourceDao resourceDao;
    @Autowired(required = false)
    ResivewDao resivewDao;
    @Autowired(required = false)
    StudentDao studentDao;

    @Autowired
    QiNiuUpload qiNiuUpload;
    /**
     * 分页查询所有课程
     * @param current 当前页码
     * @param size 每页显示条数
     * @param keyword 关键字
     * @return
     */
    @Override
    public ResultMsg findAll(int current, int size, String keyword) {
        List<TbCourse> list =courseDao.selectAll((current-1)*size,size,keyword==null?"":keyword);
        QueryWrapper<TbCourse> wrapper = new QueryWrapper<>();
        Long count = courseDao.selectCount(wrapper);
        ResultPage page = new ResultPage(list, count);
        return ResultMsg.success(page);
    }

    @Override
    public Page<TbCourse> findAll(int current, int size, int ststus) {
        QueryWrapper<TbCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("course_ststus",ststus);
        Page<TbCourse> page = new Page<>(current, size);
        Page<TbCourse> page1 = this.page(page, wrapper);
        return page1;
    }


    /**
     * 添加或修改课程
     * @param tbCourse
     * @return
     */
    @Override
    public ResultMsg update(TbCourse tbCourse) {
        this.saveOrUpdate(tbCourse);
        return ResultMsg.success();
    }

    /**
     * 根据课程id删除课程
     * @param courseId
     * @return
     */
    @Override
    public ResultMsg del(int courseId) {
        this.removeById(courseId);
        return ResultMsg.success();
    }

    /**
     * 根据分类查询分页查询课程
     * @param current 当前页
     * @param size 每页显示条数
     * @param typeId 分类编号
     * @return
     */
    @Override
    public ResultMsg findByType(int current, int size, int typeId,String keyword) {
        List<TbCourse> list=courseDao.selectByType((current-1)*size,size,typeId,keyword==null?"":keyword);
        QueryWrapper<TbCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id",typeId);
        Long count = courseDao.selectCount(wrapper);
        ResultPage page = new ResultPage(list, count);
        return ResultMsg.success(page);
    }

    /**
     * 根据课程id查询课程
     * @param courseId
     * @return
     */
    @Override
    public ResultMsg findOne(int courseId) {
        TbCourse course = this.getById(courseId);
        return ResultMsg.success(course);
    }

    /**
     * 根据老师id查询课程
     * @param teacherId
     * @return
     */
    @Override
    public ResultMsg findByTeacherId(int teacherId) {
        List<TbCourse> list=courseDao.selectByTeacherId(teacherId);
        return ResultMsg.success(list);
    }

    @Override
    public ResultMsg findOneById(int courseId) {
        TbCourse course = this.getById(courseId);
        TbTeacher tbTeacher = teacherDao.selectById(course.getTeacherId());
        course.setTbTeacher(tbTeacher);
        TbType tbType = typeDao.selectById(course.getTypeId());
        course.setTbType(tbType);
        QueryWrapper<TbChapter> wrapper = new QueryWrapper<>();
        List<TbChapter> chapterList = chapterDao.selectList(wrapper.eq("course_id", course.getCourseId()));
        for (TbChapter tbChapter : chapterList) {
            TbResource tbResource = resourceDao.selectById(tbChapter.getResourceId());
            tbChapter.setTbResource(tbResource);
        }
        course.setTbChapters(chapterList);
        QueryWrapper<TbResivew> wrap = new QueryWrapper<>();
        wrap.eq("course_id",course.getCourseId());
        List<TbResivew> tbResivews = resivewDao.selectList(wrap);
        for (TbResivew tbResivew : tbResivews) {
            TbStudent tbStudent = studentDao.selectById(tbResivew.getStudentId());
            tbResivew.setTbStudent(tbStudent);
        }
        course.setTbResivews(tbResivews);
        return ResultMsg.success(course);
    }

    @Override
    public ResultMsg findOrderPriceByType(int page, int limit, String keyword, String typeName) {
        TbType tbType = typeDao.selectTypeByName(typeName);
        Page<TbCourse> tbCoursePage = new Page<>(page,limit);
        QueryWrapper<TbCourse> wrapper = new QueryWrapper<>();
        wrapper.orderBy(true, true,"course_price");
        Page<TbCourse> page1 = courseDao.selectPage(tbCoursePage, wrapper);
        if (tbType == null){
            return ResultMsg.success(page1);
        }
        wrapper.eq("type_id",tbType.getTypeId());
        return ResultMsg.success(courseDao.selectPage(page1,wrapper));


    }

    /**
     * 根据老师id查询课程,根据课程名称模糊查询课程
     * @param keyword 课程模糊查询关键字
     * @param teacherId
     * @return
     */
    @Override
    public ResultMsg findByTeacherId(Integer teacherId,String keyword) {
        List<TbCourse> list=courseDao.selectByTeacherIdTeacherName(keyword==null?"":keyword,teacherId);
        System.out.println("list =============== " + list);
        return ResultMsg.success(list);
    }

    @Override
    public ResultMsg uploadImg(MultipartFile file) {
        try {
            String img = qiNiuUpload.upload(file);
            return ResultMsg.success(img);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.faild(201,"上传失败!");
    }

}
